
global keep_obs "if WP_NumWorkers <= 50"

********************************************************************************
* Description: Make a list of all homogenous workplace peer groups used in the 
* main analysis and define a placebo homogenous workplace peer group from list 
********************************************************************************
set seed 12345

********************************************************************************
*** PLACEBO WP HPG A: Choose among all HPG ***
********************************************************************************

* Create a list of potential placebo WP HPG and a random number which is used
* to pick an arbitrary placebo WP HPG
use  Data/data_index_workers_X_18_65_years, clear 
keep  $keep_obs
keep WP_HPG_id WP_HPG_type

egen num_obs_WP_HPG_type = count(WP_HPG_id), by(WP_HPG_type)
gen rand_no = runiform()
sort rand_no
gen rank_tmp = _n
bysort WP_HPG_type: egen rank_WP_HPG_type = rank(rank_tmp), unique
drop rand_no rank_tmp
save Data/tmp_WP_placebo, replace

* Save the number of different types of WP HPR
keep  num_obs_WP_HPG_type WP_HPG_type
duplicates drop
save Data/tmp_number_of_WP_HPG, replace

* Randomly select a placebo WP HPG and assign to index worker
use Data/data_index_workers_X_18_65_years, replace
keep  $keep_obs
merge m:1 WP_HPG_type using Data/tmp_number_of_WP_HPG, keepusing(num_obs_WP_HPG_type)
drop _merge
gen rank_WP_HPG_type = runiformint(1, num_obs_WP_HPG_type)
rename WP_HPG_id Real_WP_HPG_id
merge m:1 WP_HPG_type rank_WP_HPG_type using Data/tmp_WP_placebo
keep if _merge == 3
drop _merge

drop if WP_HPG_id == Real_WP_HPG_id
drop num_obs_WP_HPG_type rank_WP_HPG_type Real_WP_HPG_id

* Add placebo coworker data using the placebo workplace 
merge m:1 WP_HPG_id using Data/data_coworker_X_18_65_years
drop if _merge == 2
drop _merge

* Note the difference here compared to main analysis:
* No need to adjust the workplace variables (by calculate coworker peer averages
* excluding the index worker) because the index worker does not work at the 
* placebo workplace and is not included when calculating the WP variables

order PersonId WP_HPG_id elec_district2018 WorkplaceId ///
AstKommun WP_Org_Sni2007 WP_SNI_Section WP_P0846_lopnr_PeOrgNr WP_AstKommun

merge 1:1 PersonId using Data/data_ParentsVoted
drop if _merge == 2
drop _merge

save Data/data_placebo_A_analysis_coworker_peers, replace

********************************************************************************
*** PLACEBO WP HPG B: Choose among all HPG in the same municiaplity  **
********************************************************************************

* Create a list of potential placebo WP HPG and a random number which
* is used to pick an arbitrary placebo WP HPG.
* Here within the same municipality
use  Data/data_index_workers_X_18_65_years, clear
keep  $keep_obs

* WP_HPG_type and AstKommun defines potential placebo workplaces
keep WP_HPG_id WP_HPG_type AstKommun
tab AstKommun, miss
duplicates drop

* Create new indicator of WP_HPG_type_B - now also based on municiaplity of WP
* This determine the list of potential placebo workplaces for each WP
egen WP_HPG_type_B = group(WP_HPG_type AstKommun)
egen num_obs_WP_HPG_type_B = count(WP_HPG_id), by(WP_HPG_type_B)
drop WP_HPG_type

* Rank potential placebo workplaces in random order so it becomes arbitrary 
* which one we assign
gen rand_no = runiform()
sort rand_no
gen rank_tmp = _n
bysort WP_HPG_type_B: egen rank_WP_HPG_type_B = rank(rank_tmp), unique
drop rand_no rank_tmp
sort WP_HPG_type_B rank_WP_HPG_type_B

save Data/tmp_WP_placebo_B, replace

* Also save the number of different of WP HPGs of type B, ie within munic
keep  num_obs_WP_HPG_type_B WP_HPG_type_B
duplicates drop
save Data/tmp_number_of_WP_HPG_B, replace

* Randomly select a placebo WP HPG of type B and assign to index worker
use Data/data_index_workers_X_18_65_years, replace
keep  $keep_obs

egen WP_HPG_type_B = group(WP_HPG_type AstKommun)

merge m:1 WP_HPG_type_B using Data/tmp_number_of_WP_HPG_B, keepusing(num_obs_WP_HPG_type_B)
drop _merge
gen rank_WP_HPG_type_B = runiformint(1, num_obs_WP_HPG_type_B)
rename WP_HPG_id Real_WP_HPG_id

merge m:1 WP_HPG_type_B rank_WP_HPG_type_B using Data/tmp_WP_placebo_B
keep if _merge == 3
drop _merge

drop if WP_HPG_id == Real_WP_HPG_id
drop num_obs_WP_HPG_type_B rank_WP_HPG_type_B Real_WP_HPG_id

* Now each index worker has been assigned to a placebo WP HPG in the same 
* munic 
merge m:1 WP_HPG_id using Data/data_coworker_X_18_65_years
drop if _merge == 2
drop _merge

* Note the difference here compared to main analysis:
* No need to adjust the workplace variables (by calculate coworker peer averages
* excluding the index worker) because the index worker does not work at the 
* placebo workplace and is not included when calculating the WP variables

order PersonId WP_HPG_id elec_district2018 WorkplaceId ///
AstKommun WP_Org_Sni2007 WP_SNI_Section WP_P0846_lopnr_PeOrgNr WP_AstKommun

merge 1:1 PersonId using Data/data_ParentsVoted
drop if _merge == 2
drop _merge

save Data/data_placebo_B_analysis_coworker_peers, replace

********************************************************************************
*** PLACEBO WP HPG C_updated: Same municipality and same HPG size  ***
********************************************************************************

* Load index worker data and apply filter
use Data/data_index_workers_X_18_65_years, clear
keep  $keep_obs

* Add HPG size from coworker data
merge m:1 WP_HPG_id using Data/data_coworker_X_18_65_years, keepusing(WP_NumWorkers)
drop _merge

* Flag large workplaces (based on WP_NumWorkers)
gen large_WP = WP_NumWorkers >= 4
drop WP_NumWorkers

* Keep variables needed for defining placebo groups
keep WP_HPG_id WP_HPG_type AstKommun large_WP
duplicates drop

* Create new grouping variable: HPG type × municipality × size
egen WP_HPG_type_C_updated = group(WP_HPG_type AstKommun large_WP)
egen num_obs_WP_HPG_type_C_updated = count(WP_HPG_id), by(WP_HPG_type_C_updated)
drop WP_HPG_type

* Rank randomly within each group
gen rand_no = runiform()
sort rand_no
gen rank_tmp = _n
bysort WP_HPG_type_C_updated: egen rank_WP_HPG_type_C_updated = rank(rank_tmp), unique
drop rand_no rank_tmp
sort WP_HPG_type_C_updated rank_WP_HPG_type_C_updated

* Save list of possible placebo HPGs
save Data/tmp_WP_placebo_C_updated, replace

* Save number of HPGs per group
keep num_obs_WP_HPG_type_C_updated WP_HPG_type_C_updated
duplicates drop
save Data/tmp_number_of_WP_HPG_C_updated, replace

* Reload main data and assign placebo group
use Data/data_index_workers_X_18_65_years, clear
keep $keep_obs

merge m:1 WP_HPG_id using Data/data_coworker_X_18_65_years, keepusing(WP_NumWorkers)
drop if _merge == 2
drop _merge

gen large_WP = WP_NumWorkers >= 4
drop WP_NumWorkers

egen WP_HPG_type_C_updated = group(WP_HPG_type AstKommun large_WP)

merge m:1 WP_HPG_type_C_updated using Data/tmp_number_of_WP_HPG_C_updated, keepusing(num_obs_WP_HPG_type_C_updated)
drop _merge
gen rank_WP_HPG_type_C_updated = runiformint(1, num_obs_WP_HPG_type_C_updated)
rename WP_HPG_id Real_WP_HPG_id

merge m:1 WP_HPG_type_C_updated rank_WP_HPG_type_C_updated using Data/tmp_WP_placebo_C_updated
keep if _merge == 3
drop _merge
drop if WP_HPG_id == Real_WP_HPG_id
drop num_obs_WP_HPG_type_C_updated rank_WP_HPG_type_C_updated Real_WP_HPG_id

* Merge with coworker peer group data (placebo group characteristics)
merge m:1 WP_HPG_id using Data/data_coworker_X_18_65_years
drop if _merge == 2
drop _merge

* Add parent voting info
merge 1:1 PersonId using Data/data_ParentsVoted
drop if _merge == 2
drop _merge

order PersonId WP_HPG_id elec_district2018 WorkplaceId ///
AstKommun WP_Org_Sni2007 WP_SNI_Section WP_P0846_lopnr_PeOrgNr WP_AstKommun

save Data/data_placebo_C_updated_analysis_coworker_peers, replace



